/**
 * 
 */
package com.jjt.kiki.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.jjt.kiki.bean.No;



/**
 * @author jjtEatJava
 *
 */
public class NoDao extends BaseDao{
	
	public void save(List<No> list) {
		Connection conn = null;
		PreparedStatement statement =null;
		try {
			conn = this.getConnection();
			conn.setAutoCommit(false);
			statement = conn.prepareStatement("insert into tb_no (no_id,no_user_uk,fans_count,follow_count,pubshare_count,album_count,no_is_find) values(?,?,?,?,?,?,?)");
			for(No no:list) {
				UUID uuid = UUID.randomUUID();
				statement.setString(1, uuid.toString());
				statement.setString(2, no.getNoUserUk());
				statement.setInt(3, no.getFansCount());
				statement.setInt(4, no.getFollowCount());
				statement.setInt(5, no.getPubshareCount());
				statement.setInt(6, no.getAlumCount());
				statement.setInt(7, no.getNoIsFind());
				statement.addBatch();
			}
			statement.executeBatch();
			statement.close();
	        conn.setAutoCommit(true);//在把自动提交打开
			conn.close();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			} // 操作不成功，回滚事务 
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * jjtEatJava
	 * 2017年5月9日
	 * @param i
	 * @return
	 */
	public List<No> findTop(int size) {
		Connection conn = null;
		PreparedStatement statement =null;
		PreparedStatement updateStatement =null;
		List<No> list = new ArrayList<No>(100);
		try {
			conn = this.getConnection();
			statement = conn.prepareStatement("select * from tb_no  where no_is_find = 0 limit ?");
			updateStatement = conn.prepareStatement("update tb_no set no_is_find=1 where no_id =? ");
			statement.setInt(1,size);
			ResultSet set = statement.executeQuery();
			while(set.next()) {
				No no = new No();
				no.setNoId(set.getString("no_id"));
				no.setNoUserUk(set.getString("no_user_uk"));
				no.setFansCount(set.getInt("fans_count"));
				no.setFollowCount(set.getInt("follow_count"));
				no.setPubshareCount(set.getInt("pubshare_count"));
				no.setAlumCount(set.getInt("album_count"));
				no.setNoIsFind(1);
				list.add(no);
				updateStatement.setString(1, no.getNoId());
				updateStatement.addBatch();
			}
			updateStatement.executeBatch();
			updateStatement.close();
			statement.close();
			conn.close();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			try {
				statement.close();
				updateStatement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * jjtEatJava
	 * 2017年5月9日
	 * @param uk
	 * @return
	 */
	public int countByNoUserUk(String uk) {
		Connection conn = null;
		PreparedStatement statement =null;
		try {
			conn = this.getConnection();
			statement = conn.prepareStatement("select count(*) from tb_no where no_user_uk = ?");
			statement.setString(1,uk);
			ResultSet set = statement.executeQuery();
			set.next();
			int count = set.getInt(1);
			statement.close();
			conn.close();
			return count;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return -1;
	}

	/**
	 * jjtEatJava
	 * 2017年5月9日
	 * @param noId
	 */
	public void delete(String noId) {
		Connection conn = null;
		PreparedStatement statement =null;
		try {
			conn = this.getConnection();
			statement = conn.prepareStatement("delete from tb_no where no_id = ?");
			statement.setString(1,noId);
			statement.execute();
			statement.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
